/*
 * @Author: dgflash
 * @Date: 2022-07-12 15:29:32
 * @LastEditors: dgflash
 * @LastEditTime: 2022-07-12 16:11:48
 */

import { Collection, ObjectId, OptionalId } from "mongodb";
import { DbCollectionName, MongoDB } from "./MongoDB";
import { DbUser } from "../../tsrpc/shared/types/DbUser";





/** 用户数据表逻辑 */
export class User {
    private static c: Collection<OptionalId<DbUser>> = null!;

    static init() {
        this.c = MongoDB.db.collection(DbCollectionName.user);
    }

    /**
     * 注册新玩家
     * @param account 账号
     * @returns 玩家自增唯一编号
     */
    static addAccount(account: string,password:string): Promise<number> {
        return new Promise(async (resolve: Function, reject: Function) => {
            var key = await MongoDB.getNextSequenceValue(DbCollectionName.user);
            // 插入用户数据
            await this.c.insertOne({
                key,
                account:account,
                password:password,
                createtime: new Date(),
                // pos: { x: 0, y: 1.7, z: 0 },                        // 初始位置
                // rotation: { x: 0, y: 0, z: 0, w: 1 }                // 初始旋转
            });

            // 返回客户端结果
            resolve(key);
        });
    }

    /**
     * 通过玩家名删除玩家数据
     * @param account  玩家名
     * @returns 是否删除成功
     */
    static delUserByAccount(account: string): Promise<boolean> {
        return new Promise(async (resolve: Function, reject: Function) => {
            var ret = await this.c.deleteOne({ account: account });
            if (ret.acknowledged && ret.deletedCount > 0)
                resolve(true);
            else
                resolve(false);
        });
    }

    /**
      * 删除所有玩家数据
      * @returns 是否删除成功
      */
    static delUserByAll() {
        return new Promise(async (resolve: Function, reject: Function) => {
            var ret = await this.c.deleteMany({});
            if (ret.acknowledged && ret.deletedCount > 0)
                resolve(true);
            else
                resolve(false);
        });
    }

    /** 修改玩家状态数据 */
    // static updatePlayerState(_id: ObjectId, pos: RolePosition, rotation: RoleRotation) {
    //     return new Promise(async (resolve: Function, reject: Function) => {
    //         this.c.updateOne(
    //             {
    //                 _id
    //             },
    //             {
    //                 $set: {
    //                     pos,
    //                     rotation
    //                 }
    //             }
    //         ).then(ret => {
    //             if (ret.acknowledged && ret.modifiedCount > 0)
    //                 resolve(true);
    //             else
    //                 resolve(false);
    //         });
    //     });
    // }

    /**
     * 通过账号名获取玩家数据
     * @param account  玩家名
     * @returns 玩家数据
     */
    static getUserByAccount(account: string): Promise<DbUser> {
        return new Promise(async (resolve: Function, reject: Function) => {
            var dUser = await this.c.findOne({ account: account });
            resolve(dUser);
        });
    }
}

declare module 'tsrpc' {
    export interface ApiCall {
        user?: DbUser;
    }
}